<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户挂号</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .image{
            padding:30px;
            width: 95%;
        }
        .image li{
            display: block;
            float: left;
            border: #CCCCCC solid 1px;
            height: 400px;
            width: 230px;
            text-align: center;
            border-radius: 12px;
            margin: 30px;
        }
        .xiang{
            line-height: 400px;
            cursor:pointer;
        }
        #dang{
            height: 50px;
            width: 500px;
            line-height: 50px;
            margin-left: 30px;
        }
    </style>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" id="sel">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户信息:</label>
            <div class="layui-input-inline">
                <input type="text" name="reportName" id="im" placeholder="请输入你要查询的信息"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" id="doSearch">查询</button>
            </div>
        </div>
    </div>

</form>
<div id="dang">
    <input type="radio" id="bang1"  name="dang" value="1" title="" >&nbsp;&nbsp;&nbsp;&nbsp;查看全部&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" id="bang2" name="dang" value="2" title="" >&nbsp;&nbsp;&nbsp;&nbsp;当天挂号&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" id="bang3"  name="dang" value="3" title="">&nbsp;&nbsp;&nbsp;&nbsp;预约挂号&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="hidden" id="ban" />
<!--    th:value="${session.ban}"-->
</div>
<div id="all">
    <ul class="image">
        <li class="xiang" lay-event="add"><span style="font-size: 150px">+</span></li>
<!--        /th:each="map:${report}"-->
        <ul id="patientList"></ul>
        <li id="template" style="line-height: 40px; display: none;">
            <form class="layui-form" lay-filter="reall" id="reall" name="reall">
                <div class="layui-form-item" style="height: 30px;line-height: 30px;">
                    <label class="layui-form-label" style="text-align: right">就诊号:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="reportId"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top: -5px;">
                    <label class="layui-form-label" style="text-align: right">患者姓名:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="reportName"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:-10px;">
                    <label class="layui-form-label" style="text-align: right">手机号:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="phone"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:-10px;">
                    <label class="layui-form-label" style="text-align: right">身份证号:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="carido"></span><span>****</span><span class="caridt"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:-10px;">
                    <label class="layui-form-label" style="text-align: right">挂号类型:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="type"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:-10px;">
                    <label class="layui-form-label" style="text-align: right">挂号时间:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="datime"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:-10px;">
                    <label class="layui-form-label">所属科室:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="department"></span>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:-10px;">
                    <label class="layui-form-label">就诊医生:</label>
                    <div class="layui-input-block" style="text-align: left">
                        <span class="doctorName"></span>
                    </div>
                </div>
                <button type="button" style="width:100px;margin-top:-10px;" class="layui-btn delre">删除</button>
                <button type="button" style="width:100px;margin-top:-10px;" class="layui-btn zhuanyuan">转住院</button>
            </form>
        </li>
    </ul>


</div>
<div style="display: none;padding-top: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form"  lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item" style="margin-left: 0">
            <div class="layui-inline">
                <label class="layui-form-label">姓名:</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id">
                    <input type="text" name="reportName"  lay-verify="required"   placeholder="请输入用户姓名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="男" title="男" checked>
                    <input type="radio" name="sex" value="女" title="女" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">年龄:</label>
                <div class="layui-input-inline">
                    <input type="text" name="age" id="age" lay-verify="required|number"   placeholder="请输入用户年龄" autocomplete="off"
                           class="layui-input">
<!--                    th:value="${session.yonghu}"-->
                    <input type="hidden" name="users" />
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">电话:</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" id="phon"  lay-verify="required|phone|number"   placeholder="请输入用户电话" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">身份证号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="carid" id="idcard" lay-verify="required|identity|number"  placeholder="请输入用户身份证号" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">科室：</label>
                <div class="layui-input-inline">
                    <select name="department" id="departmentId"  lay-verify="required" lay-filter="departmentId">
                        <option value=""></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">挂号类型:</label>
                <div class="layui-input-inline">
                    <select name="reporttype" id="registeredId" lay-verify="required" lay-filter="registeredId">
                        <option value=""></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">医生:</label>
                <div class="layui-input-inline">
                    <select name="doctor" id="doctorId" lay-verify="required" lay-filter="doctorId">
                        <option value="" class="doct"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">挂号费:</label>
                <div class="layui-input-inline">
                    <input type="text" name="price" id="typemoney" lay-verify="required" autocomplete="off"
                           class="layui-input" readonly>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-left: 0">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>
<div id="zhaunyu" style="display:none;padding-left: 20px;">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">请填写病因:</label>
            <div class="layui-input-inline">
                <input type="hidden" name="areaId">
                <input type="text" name="areaName" id="bingying"  lay-verify="required"   placeholder="请输入病因" autocomplete="off"
                       class="layui-input ">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="text-align: center;">
        <div class="layui-input-block" style=" margin-left:0px">
            <input type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release"  id="zhuncheng" value="提交">
            <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
        </div>
    </div>
</div>


<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>-->
<script type="text/javascript" >

    var tableIns;
    layui.use([ 'jquery', 'layer', 'form', 'table' ], function() {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var list = [];
        //模糊查询
        $("#doSearch").click(function(){
            list = [];
            var name=$("#im").val();
            var cc= $('input[name="dang"]:checked').val();
            $.ajax({
                //要跳转的地址
                url: '/web/cao/index',
                data:{"params":name,"cc":cc},
                //数据类型
                dataType: 'json',
                //提交类型
                type: 'post',
                //查询成功后执行的方法
                success: function (data) {
                    if (data.code == 200) {
                        $('#patientList li').remove();

                        list = data.entity;
                        const $template = $('#template');
                        $.each(list, function (index, item) {
                            const $clone = $template.clone().removeAttr('id').show();
                            $clone.find('.reportId').text(item.reportId);
                            $clone.find('.reportName').text(item.reportName);
                            $clone.find('.phone').text(item.phone);
                            $clone.find('.carido').text(item.carido);
                            $clone.find('.caridt').text(item.caridt);
                            $clone.find('.type').text(item.type);
                            $clone.find('.datime').text(item.datime);
                            $clone.find('.department').text(item.department);
                            $clone.find('.doctorName').text(item.doctorName);
                            $('#patientList').append($clone);
                        });
                    }
                }
            })
            // location.href ="index?params="+name+"&cc="+cc;
        });
        //单选框选择当天的和全部的病人
        $("input[name='dang']").click(function(){
            list = [];
            console.log("点击了")
            var name=$("#im").val();
            var cc= $('input[name="dang"]:checked').val();
            // alert(cc);
            $.ajax({
                //要跳转的地址
                url: '/web/cao/index',
                data:{"params":name,"cc":cc},
                //数据类型
                dataType: 'json',
                //提交类型
                type: 'post',
                //查询成功后执行的方法
                success: function (data) {
                    if (data.code == 200) {
                        $('#patientList li').remove();

                        list = data.entity;
                        const $template = $('#template');
                        $.each(list, function (index, item) {
                            const $clone = $template.clone().removeAttr('id').show();
                            $clone.find('.reportId').text(item.reportId);
                            $clone.find('.reportName').text(item.reportName);
                            $clone.find('.phone').text(item.phone);
                            $clone.find('.carido').text(item.carido);
                            $clone.find('.caridt').text(item.caridt);
                            $clone.find('.type').text(item.type);
                            $clone.find('.datime').text(item.datime);
                            $clone.find('.department').text(item.department);
                            $clone.find('.doctorName').text(item.doctorName);
                            $('#patientList').append($clone);
                        });
                    }
                }
            })
            // location.href ="index?params="+name+"&cc="+cc;
        })
        $(function (){
            var ban=$("#ban").val();
            if(ban==1||ban==''){
                $("#bang1").prop("checked",true);
            }else if(ban==2){
                $("#bang2").prop("checked",true);
            }else{
                $("#bang3").prop("checked",true);
            }
        });

        $(document).ready(function () {
            list=[];
            var name=$("#im").val();
            var cc= $('input[name="dang"]:checked').val();
            $.ajax({
                //要跳转的地址
                url: '/web/cao/index',
                data:{"params":name,"cc":cc},
                //数据类型
                dataType: 'json',
                //提交类型
                type: 'post',
                //查询成功后执行的方法
                success: function (data) {
                    if (data.code == 200) {
                        console.log("data", data)
                        list = data.entity;

                        const $template = $('#template');
                        console.log("$template", $template.length); // 确认是否找到了模板元素，应输出 1
                        $.each(list, function (index, item) {
                            const $clone = $template.clone().removeAttr('id').show();
                            console.log("item", $clone.find('.reportId').val(item.reportId))
                            $clone.find('.reportId').text(item.reportId);
                            $clone.find('.reportName').text(item.reportName);
                            $clone.find('.phone').text(item.phone);
                            $clone.find('.carido').text(item.carido);
                            $clone.find('.caridt').text(item.caridt);
                            $clone.find('.type').text(item.type);
                            $clone.find('.datime').text(item.datime);
                            $clone.find('.department').text(item.department);
                            $clone.find('.doctorName').text(item.doctorName);

                            // 绑定按钮事件（可选）
                            $clone.find('.delre').click(function () {
                                // alert("删除就诊号：" + item.reportId);
                                //删除
                                $(document).on("click",".delre",function(){
                                    var params=$(this).prev().val();
                                    $.post("/web/caoout/selch",{reportId:params},function(res) {
                                        if (res == 0) {
                                            layer.confirm('真的删除这个用户吗', function (index) {
                                                $.post('/web/cao/delre',{'id':params},function(obj){
                                                    layer.msg(obj);
                                                    location.reload();
                                                })
                                            })
                                        }else{
                                            layer.msg("该患者已经就诊请缴费后进行操作");
                                        }
                                    })
                                })

                            });

                            $clone.find('.zhuanyuan').click(function () {
                                // 移除重复绑定，直接处理转院逻辑
                                var now = new Date();
                                var year = now.getFullYear(); //得到年份
                                var month = now.getMonth();//得到月份
                                var date = now.getDate();//得到日期
                                console.log("this", this);
                                var params = $(this).closest('li').find('.reportName').text();
                                // 修改为获取 reportId 的值
                                var ids = $(this).closest('li').find('.reportId').text();
                                console.log("ids", ids)
                                // 使用 closest 方法找到父级 li 元素，再查找 datime 类的元素获取挂号时间
                                var dat = $(this).closest('li').find('.datime').text();
                                var da = year + "-" + (month + 1) + "-" + date; // 月份需要加 1，因为 getMonth() 返回 0 - 11

                                // 解析挂号时间
                                var now2;
                                try {
                                    now2 = new Date(dat);
                                } catch (e) {
                                    console.error('挂号时间解析失败:', e);
                                    layer.msg('挂号时间信息有误，请检查');
                                    return;
                                }
                                var year2 = now2.getFullYear(); //得到年份
                                var month2 = now2.getMonth();//得到月份
                                var date2 = now2.getDate();//得到日期
                                var da2 = year2 + "-" + (month2 + 1) + "-" + date2; // 月份需要加 1，因为 getMonth() 返回 0 - 11

                                console.log("da", da);
                                console.log("da2", da2);
                                if (da !== da2) {
                                    layer.msg("对不起您选择的不是当天的挂号");
                                } else {
                                    //查询该用户处方上是否有药品
                                    $.post("/web/caoout/selch", {reportId: ids}, function (res) {
                                        if (res == 0) {
                                            //判断该用户是否有缴费未做的项目
                                            $.post("/web/caocc/seljiao", {"reid": ids}, function (res) {
                                                if (res > 0) {
                                                    layer.msg("你还有未缴费的项目请缴费后在进行该操作");
                                                } else {
                                                    $.post("/web/caocc/selwei", {"reid": ids}, function (res) {
                                                        if (res == 1) {
                                                            layer.confirm('你确定要【' + params + '】转入住院部吗', function (index) {
                                                                $.post("/web/caocc/selbing", {"reid": ids}, function (obj) {
                                                                    $("#bingying").val(obj);
                                                                });
                                                                mainIndex = layer.open({
                                                                    type: 1,
                                                                    title: '转入住院部',
                                                                    content: $("#zhaunyu"),
                                                                    area: ['400px', '200px']
                                                                });
                                                            });
                                                        } else {
                                                            layer.confirm("你有项目未做是否继续操作", function () {
                                                                layer.confirm('你确定要【' + params + '】转入住院部吗', function (index) {
                                                                    $.post("/web/caocc/selbing", {"reid": ids}, function (obj) {
                                                                        $("#bingying").val(obj);
                                                                    });
                                                                    mainIndex = layer.open({
                                                                        type: 1,
                                                                        title: '转入住院部',
                                                                        content: $("#zhaunyu"),
                                                                        area: ['400px', '200px']
                                                                    });
                                                                });
                                                            });
                                                        }
                                                    });
                                                }
                                            });
                                            $("#zhuncheng").click(function () {
                                                console.log("点击了转院", ids);
                                                var zh = $("#bingying").val();
                                                $.post('/web/cao/zhuanyuan', {'id': ids, 'zhuan': zh}, function (obj) {
                                                    layer.msg("转出成功");
                                                    location.reload();
                                                });
                                            });
                                        } else {
                                            layer.msg("该患者已经就诊请缴费后进行操作");
                                        }
                                    });
                                }
                            });

                            $('#patientList').append($clone);
                        });
                    }
                }
            })


        });
        //挂号类型的id
        var registeredId=null;
        //科室id
        var departmentId=null;
        //查看选中科室id
        form.on('select(departmentId)', function(data){
            departmentId=data.value;//得到被选中的值
            //获取select属性下的所有
            var doctorId1 = document.getElementById("doctorId");
            doctorId1.length=0;
            if(registeredId==null){
                return false;
            }else{
                //渲染下拉框中的值
                $.ajax({
                    //要跳转的地址
                    url: '/web/cao/seldoctor',
                    data:{"registeredid":registeredId,"departmentId":departmentId},
                    //数据类型
                    dataType: 'json',
                    //提交类型
                    type: 'post',
                    //查询成功后执行的方法
                    success: function (data) {
                        $.each(data, function (index, item) {
                            $('#doctorId').append(new Option(""));
                            $('#doctorId').append(new Option(item.doctorName, item.doctorId));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    }
                })
            }
        });
        //查看选中的挂号类型的id
        form.on('select(registeredId)', function(data){
            registeredId=data.value;//得到被选中的值
            $.ajax({
                //要跳转的地址
                url: '/web/cao/seltymo',
                data:{"registeredId":registeredId},
                //数据类型
                dataType: 'json',
                //提交类型
                type: 'post',
                //查询成功后执行的方法
                success: function (data) {
                    $('#typemoney').val(data);
                }
            })
            var doctorId1 = document.getElementById("doctorId");
            doctorId1.length=0;
            if(departmentId==null){
                return false;
            }else{
                //alert("科室"+departmentId+"类型"+registeredId);
                //渲染下拉框中的值
                $.ajax({
                    //要跳转的地址
                    url: '/web/cao/seldoctor',
                    data:{"registeredid":registeredId,"departmentId":departmentId},
                    //数据类型
                    dataType: 'json',
                    //提交类型
                    type: 'post',
                    //查询成功后执行的方法
                    success: function (data) {
                        $.each(data, function (index, item) {
                            $('#doctorId').append(new Option(item.doctorName, item.doctorId));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    }
                })
            }
        });
        //渲染科室下拉框中的值
        $.ajax({
            //要跳转的地址
            url: '/web/cao/seldep',
            //数据类型
            dataType: 'json',
            //提交类型
            type: 'post',
            //查询成功后执行的方法
            success: function (data) {
                $.each(data, function (index, item) {
                    $('#departmentId').append(new Option(item.department, item.departmentId));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法
            }
        })
        //渲染挂号类型的下拉框
        $.ajax({
            //要跳转的地址
            url: '/web/cao/selreg',
            //数据类型
            dataType: 'json',
            //提交类型
            type: 'post',
            //查询成功后执行的方法
            success: function (data) {
                $.each(data, function (index, item) {
                    $('#registeredId').append(new Option(item.type, item.registeredId));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法
            }
        })

        var url;
        var mainIndex;
        //打开添加页面
        $(".xiang").click(function(){
            mainIndex=layer.open({
                type:1,
                title:'添加病人',
                content:$("#saveOrUpdateDiv"),
                area:['400px','550px'],
                success:function(index){
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    url="/web/cao/addre";
                }
            });
        });
        //保存
        form.on("submit(doSubmit)",function(obj){
            var phone=$("#phon").val();
            var carid=$("#idcard").val();
            var age=$("#age").val();
            //序列化表单数据
            var ad=$("#doctorId").val();
            if(ad==null){
                layer.msg("请选择要就诊的医生")
            }else if(age<=0){
                layer.msg("请输入正确的年龄")
            }else{
                $.post("/web/cao/phone",{"phone":phone,"carid":carid},function(obj){
                    if(obj==1){
                        layer.msg("该手机号的患者已存在");
                    }else if(obj==2){
                        layer.msg("该身份证号的患者已存在");
                    }else if(obj==3){
                        var params=$("#dataFrm").serialize();
                        $.post(url,params,function(obj){
                            layer.msg(obj);
                            //关闭弹出层
                            layer.close(mainIndex)
                            //刷新数据 表格
                            location.reload();
                            console.log("obj", obj)

                            // $.post('/web/cao/report',params,function(res){
                            //     console.log("res", res)
                            // })
                        })
                    }
                })

            }
        });


    })

</script>
</body>
</html>
